package com.taotao.rest.service;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.taotao.common.utils.JsonUtils;
import com.taotao.mapper.TbItemDescMapper;
import com.taotao.mapper.TbItemMapper;
import com.taotao.mapper.TbItemParamItemMapper;
import com.taotao.pojo.TbItem;
import com.taotao.pojo.TbItemDesc;
import com.taotao.pojo.TbItemParamItem;
import com.taotao.pojo.TbItemParamItemExample;
import com.taotao.pojo.TbItemParamItemExample.Criteria;
import com.taotao.rest.component.JedisClient;

@Service
public class ItemServiceImpl implements ItemService {
	@Autowired
	private TbItemMapper tbItemMapper;
	@Autowired
	private JedisClient jedisClient;
	@Value("${REDIS_ITEM_KEY}")
	private String REDIS_ITEM;
	@Value("${ITEM_BASE_INFO_KEY}")
	private String ITEM_BASE_INFO;
	@Value("${ITEM_EXPIRE_SECOND}")
	private Integer ITEM_EXPIRE_SECOND;
	@Value("${ITEM_DESC_KEY}")
	private String ITEM_DESC;
	@Value("${ITEM_PARAM_KEY}")
	private String ITEM_PARAM;

	@Override
	public TbItem getItemById(long itemId) {
		// 查询前判断redis中是否存在对应商品key的缓存数据,有则直接从缓存获取
		try {
			String itemInfo = jedisClient.get(REDIS_ITEM + ":" + itemId + ":"
					+ ITEM_BASE_INFO);
			if (StringUtils.isNotEmpty(itemInfo)) {
				return JsonUtils.jsonToPojo(itemInfo, TbItem.class);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		// 查询数据库获取商品信息
		TbItem tbItem = tbItemMapper.selectByPrimaryKey(itemId);

		// 将商品信息存入redis服务器缓存中,并设置过期时间
		try {
			// 添加缓存
			jedisClient.set(REDIS_ITEM + ":" + itemId + ":" + ITEM_BASE_INFO,
					JsonUtils.objectToJson(tbItem));
			// 设置过期时间
			jedisClient.expire(
					REDIS_ITEM + ":" + itemId + ":" + ITEM_BASE_INFO,
					ITEM_EXPIRE_SECOND);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return tbItem;
	}

	@Autowired
	private TbItemDescMapper tbItemDescMapper;

	/**
	 * 获取商品描述
	 */
	@Override
	public TbItemDesc getItemDescById(long itemId) {
		try {
			String json = jedisClient.get(REDIS_ITEM + ":" + itemId + ":"
					+ ITEM_DESC);
			if (StringUtils.isNotEmpty(json)) {
				return JsonUtils.jsonToPojo(json, TbItemDesc.class);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		TbItemDesc tbItemDesc = tbItemDescMapper.selectByPrimaryKey(itemId);
		try {
			jedisClient.set(REDIS_ITEM + ":" + itemId + ":" + ITEM_DESC,
					JsonUtils.objectToJson(tbItemDesc));
			jedisClient.expire(REDIS_ITEM + ":" + itemId + ":" + ITEM_DESC,
					ITEM_EXPIRE_SECOND);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return tbItemDesc;
	}

	/**
	 * 获取商品规格参数
	 */
	@Autowired
	private TbItemParamItemMapper itemParamItemMapper;

	@Override
	public TbItemParamItem getItemParamItemByItemId(long itemId) {
		try {
			String json = jedisClient.get(REDIS_ITEM + ":" + itemId + ":"
					+ ITEM_PARAM);
			if (StringUtils.isNotEmpty(json)) {
				return JsonUtils.jsonToPojo(json, TbItemParamItem.class);
			}
		} catch (Exception e) {
			// TODO: handle exception
		}
		TbItemParamItemExample example = new TbItemParamItemExample();
		Criteria criteria = example.createCriteria();
		criteria.andItemIdEqualTo(itemId);
		List<TbItemParamItem> list = itemParamItemMapper
				.selectByExampleWithBLOBs(example);
		if (list != null && list.size() > 0) {
			TbItemParamItem tbItemParamItem = list.get(0);
			try {
				jedisClient.set(REDIS_ITEM + ":" + itemId + ":" + ITEM_PARAM,
						JsonUtils.objectToJson(tbItemParamItem));
				jedisClient.expire(
						REDIS_ITEM + ":" + itemId + ":" + ITEM_PARAM,
						ITEM_EXPIRE_SECOND);
			} catch (Exception e) {
				e.printStackTrace();
			}
			return tbItemParamItem;
		}
		return null;
	}

}
